
import BaseView from "../baseView.js"


//编辑界面
export default class UpdateStudentView extends BaseView{
     //重写子类的重构函数 
    constructor(id){
     super();
      console.log("UpdateStudentView被创建")
      //创建一个属性
      this.stuid = id;
      console.log("UpdateStudentView==id==>",this.stuid )
      //初始化
      this.init();
    }


    render(){
        $("#showContent").html(
            `<h1>编辑界面</h1>
            <hr/>
            <form class="layui-form" >
  <div class="layui-form-item">
    <label class="layui-form-label">姓名</label>
    <div class="layui-input-inline">
      <input type="text" id="name" required  lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">年龄</label>
    <div class="layui-input-inline">
      <input type="text" id="age" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">地址：</label>
    <div class="layui-input-inline">    
      <select id="city" lay-verify="required">
        <option value="成都市">成都市</option>
        <option value="德阳市">德阳市</option>
        <option value="自贡市">自贡市</option>
        <option value="攀枝花市">攀枝花市</option>
      </select>
    </div>
  </div>
  
 
  <div class="layui-form-item">
    <label class="layui-form-label">性别</label>
    <div class="layui-input-block">
      <input type="radio" name="sex" value="男" title="男">
      <input type="radio" name="sex" value="女" title="女" checked>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button type='button' id='updateBtn' class="layui-btn" lay-submit lay-filter="formDemo">修改</button>
      <button type="button" id="goback" class="layui-btn layui-btn-primary">返回</button>
    </div>
  </div>
</form> 
 `
        )
    }

    handle(){
        //console.log("stuid==>>",this.stuid);
        //查询学生的详细信息
        //let stuid = this.stuid
        let that = this;
     layui.use(['form'],()=>{
        let  form = layui.form; 
        $.ajax({
            type:"get",
            url:`/api/student/showDeailInfo/${that.stuid}`,
            dataType:"json",
            success:(data)=>{
               console.log(data,"-----");
               $("#name").val(data.name);
               $("#age").val(data.age);
               //设置地址
               $(`option[value=${data.address}]`).attr("selected",true);
            //设置性别的值
            console.log(data.gender=="男","-----")
            data.gender=="男"?$(":radio[value='男']").attr("checked",true):$(":radio[value='女']").attr("checked",true)
              /*  if(data.gender=="男"){
                $("radio[value='男']").attr("checked",true)
               } else{
                $("radio[value='女']").attr("checked",true)
               } */
               //重新渲染 表单
             form.render();

            }
        })

     })

     //返回
     $("#goback").click(()=>{
        window.location.href="#/main/studentlist"
     });

     //修改
     $("#updateBtn").click(()=>{
        console.log('aaaa');
      //修改数据
      $.ajax({
        type:"put",
        url:"/api/student/updatestudentinfo",
        data:{
            stuid:that.stuid,
            name:$("#name").val(),
            age:$("#age").val(),
            gender:$("input:checked").val(),
            address :$("#city").val()
        },
        dataType:"json",
        success:(data)=>{
             console.log(data);
             if(data.code==0){
                    alert(data.msg);
                    //跳转到主界面
                    window.location.href="#/main/studentlist";
             }

        }
      })

     })


        
    }
}




